package com.bnz.common.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.concurrent.CompletableFuture;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class SendJmsUtil {

	public static void sendCode(String accessId,String accessKeySecret,String signName,String templateCode,String phone,String paramCode){
		StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
				.accessKeyId(accessId)
				.accessKeySecret(accessKeySecret)
				//.securityToken("<your-token>") // use STS token
				.build());

		// Configure the Client
		AsyncClient client = AsyncClient.builder()
				.region("cn-hangzhou") // Region ID
				.credentialsProvider(provider)
				.overrideConfiguration(
						ClientOverrideConfiguration.create()
								.setEndpointOverride("dysmsapi.aliyuncs.com")
				)
				.build();

		SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
				.signName(signName)
				.templateCode(templateCode)
				.phoneNumbers(phone)
				.templateParam(paramCode)
				.build();

		CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
		// Synchronously get the return value of the API request
		// SendSmsResponse resp = response.get();
		// System.out.println(new Gson().toJson(resp));
		// Asynchronous processing of return values
        /*response.thenAccept(resp -> {
            System.out.println(new Gson().toJson(resp));
        }).exceptionally(throwable -> { // Handling exceptions
            System.out.println(throwable.getMessage());
            return null;
        });*/

		// Finally, close the client
		client.close();
	}
}
